﻿package mortal.game.manager
{
    import __AS3__.vec.*;
    import com.gengine.global.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.component.window.*;
    import mortal.mvc.interfaces.*;

    public class WindowLayer extends UILayer
    {
        private var dcPopingWindow:Dictionary;
        public static var topLayerChangeHander:Function;

        public function WindowLayer()
        {
            this.dcPopingWindow = new Dictionary(true);
            return;
        }// end function

        override public function setTop(param1:DisplayObject) : void
        {
            super.setTop(param1);
            if (topLayerChangeHander != null)
            {
                topLayerChangeHander.call(this, this);
            }
            return;
        }// end function

        override protected function layerChange() : void
        {
            this.dispatchEvent(new WindowEvent(WindowEvent.WINDOWLEVELCHANGE));
            return;
        }// end function

        override public function addPopUp(param1:DisplayObject, param2:Boolean = false) : void
        {
            var tl:TweenLite;
            var displayObject:* = param1;
            var modal:* = param2;
            if (displayObject && (this.contains(displayObject) == false || this.isPoping(displayObject)))
            {
                if (displayObject.localToGlobal(new Point()).x + 100 > Global.stage.stageWidth)
                {
                    this.centerPopup(displayObject);
                }
                this.addChild(displayObject);
                this.setTop(displayObject);
                this.layerChange();
                if (displayObject is IView)
                {
                    displayObject.alpha = 0;
                    tl = new TweenLite(displayObject, 0.3, {alpha:1, ease:Cubic.easeOut, onComplete:function () : void
            {
                removePoping(displayObject);
                if (displayObject is Window)
                {
                    displayObject.dispatchEvent(new WindowEvent(WindowEvent.SHOW_END));
                }
                return;
            }// end function
            });
                    this.addPoping(displayObject, tl);
                }
            }
            return;
        }// end function

        override public function removePopup(param1:DisplayObject, param2:Boolean = true) : void
        {
            var tl:TweenLite;
            var displayObject:* = param1;
            var tweenable:* = param2;
            if (this.contains(displayObject))
            {
                if (tweenable)
                {
                    this.setObjMouseEnable(displayObject, false);
                    tl = new TweenLite(displayObject, 0.3, {alpha:0, ease:Cubic.easeOut, onComplete:function () : void
            {
                removePoping(displayObject);
                DisplayUtil.removeMe(displayObject);
                displayObject.alpha = 1;
                layerChange();
                setObjMouseEnable(displayObject, true);
                if (displayObject is Window)
                {
                    displayObject.dispatchEvent(new WindowEvent(WindowEvent.CLOSE_END));
                }
                return;
            }// end function
            });
                    this.addPoping(displayObject, tl);
                }
                else
                {
                    this.removePoping(displayObject);
                    removeChild(displayObject);
                    this.layerChange();
                    this.setObjMouseEnable(displayObject, true);
                }
            }
            return;
        }// end function

        protected function addPoping(param1:Object, param2:TweenLite) : void
        {
            this.removePoping(param1);
            this.dcPopingWindow[param1] = param2;
            param2.play();
            return;
        }// end function

        protected function isPoping(param1:Object) : Boolean
        {
            var _loc_2:* = this.dcPopingWindow[param1];
            return _loc_2 != null;
        }// end function

        protected function removePoping(param1:Object) : void
        {
            var _loc_2:TweenLite = null;
            if (this.isPoping(param1))
            {
                _loc_2 = this.dcPopingWindow[param1];
                _loc_2.pause();
                _loc_2.kill();
                _loc_2 = null;
                this.setObjMouseEnable(param1, true);
                delete this.dcPopingWindow[param1];
            }
            return;
        }// end function

        private function setObjMouseEnable(param1:Object, param2:Boolean) : void
        {
            if (param1 is InteractiveObject)
            {
                (param1 as InteractiveObject).mouseEnabled = param2;
            }
            if (param1 is DisplayObjectContainer)
            {
                (param1 as DisplayObjectContainer).mouseChildren = param2;
            }
            return;
        }// end function

        override public function centerPopup(param1:DisplayObject) : void
        {
            param1.x = (Global.stage.stageWidth - param1.width) / 2;
            param1.y = (Global.stage.stageHeight - param1.height - 30) / 2;
            if (param1.x < 0)
            {
                param1.x = 0;
            }
            if (param1.y < 0)
            {
                param1.y = 0;
            }
            return;
        }// end function

        public function leftToChat(param1:DisplayObject) : void
        {
            if (!this.contains(param1) || !(param1 is IView))
            {
                return;
            }
            param1.x = 280;
            return;
        }// end function

        public function rightToTask(param1:DisplayObject) : void
        {
            if (!this.contains(param1))
            {
                return;
            }
            param1.x = Global.stage.stageWidth - param1.width - 280;
            return;
        }// end function

        public function centerWindows(... args) : void
        {
            var _loc_3:Vector.<Window> = null;
            var _loc_8:int = 0;
            args = 0;
            _loc_3 = new Vector.<Window>;
            var _loc_4:int = 10000;
            var _loc_5:Number = 5;
            var _loc_6:int = 0;
            while (_loc_6 < args.length)
            {
                
                if (!(args[_loc_6] is Window))
                {
                    if (args[_loc_6] is Number)
                    {
                        _loc_5 = args[_loc_6];
                    }
                }
                else
                {
                    _loc_3.push(args[_loc_6] as Window);
                    args = args + (args[_loc_6] as Window).width;
                    this.centerPopup(args[_loc_6] as Window);
                    _loc_4 = Math.min(_loc_4, (args[_loc_6] as Window).y);
                }
                _loc_6++;
            }
            args = args + (_loc_3.length - 1) * _loc_5;
            var _loc_7:* = (Global.stage.stageWidth - args) / 2;
            var _loc_9:int = 0;
            while (_loc_9 < _loc_3.length)
            {
                
                _loc_8 = (Global.stage.stageHeight - _loc_3[_loc_9].height) / 2;
                if (_loc_3[_loc_9].isHide)
                {
                    _loc_3[_loc_9].show();
                }
                this.setTop(_loc_3[_loc_9]);
                _loc_3[_loc_9].x = _loc_7;
                _loc_3[_loc_9].y = _loc_8;
                _loc_7 = _loc_7 + (_loc_3[_loc_9].width + _loc_5);
                _loc_9++;
            }
            return;
        }// end function

        public function tweenCenterWindow(... args) : void
        {
            var _loc_9:int = 0;
            args = 0;
            var _loc_3:* = new Vector.<Window>;
            var _loc_4:* = new Dictionary();
            var _loc_5:int = 10000;
            var _loc_6:Number = 5;
            var _loc_7:int = 0;
            while (_loc_7 < args.length)
            {
                
                if (!(args[_loc_7] is Window))
                {
                    if (args[_loc_7] is Number)
                    {
                        _loc_6 = args[_loc_7];
                    }
                }
                else
                {
                    _loc_3.push(args[_loc_7] as Window);
                    args = args + (args[_loc_7] as Window).width;
                    if ((args[_loc_7] as Window).isHide)
                    {
                        this.centerPopup(args[_loc_7] as Window);
                    }
                    _loc_4[args[_loc_7]] = new Point(args[_loc_7].x, args[_loc_7].y);
                    _loc_5 = Math.min(_loc_5, (args[_loc_7] as Window).y);
                }
                _loc_7++;
            }
            args = args + (_loc_3.length - 1) * _loc_6;
            var _loc_8:* = (Global.stage.stageWidth - args) / 2;
            var _loc_10:int = 0;
            while (_loc_10 < _loc_3.length)
            {
                
                _loc_9 = (Global.stage.stageHeight - _loc_3[_loc_10].height) / 2;
                if (_loc_3[_loc_10].isHide)
                {
                    _loc_3[_loc_10].show(_loc_8, _loc_9);
                }
                else
                {
                    this.tweenWindow(_loc_3[_loc_10], _loc_4[_loc_3[_loc_10]].x, _loc_4[_loc_3[_loc_10]].y, _loc_8, _loc_9, 0.3);
                }
                _loc_8 = _loc_8 + (_loc_3[_loc_10].width + _loc_6);
                _loc_10++;
            }
            return;
        }// end function

        public function tweenWindow(param1:Window, param2:Number, param3:Number, param4:Number, param5:Number, param6:Number = 3) : void
        {
            param1.x = param2;
            param1.y = param3;
            TweenLite.to(param1, param6, {x:param4, y:param5, alpha:1});
            return;
        }// end function

        public function removeAllWindow() : void
        {
            var _loc_3:Window = null;
            var _loc_1:* = this.numChildren;
            var _loc_2:* = _loc_1 - 1;
            while (_loc_2 >= 0)
            {
                
                _loc_3 = this.getChildAt(_loc_2) as Window;
                if (_loc_3)
                {
                    _loc_3.hide();
                }
                _loc_2 = _loc_2 - 1;
            }
            return;
        }// end function

    }
}
